import json

from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.template.context_processors import request
from rest_framework.decorators import api_view

from Day03 import paramCheck, xuleihua
from Day03.models import login
from Day03.users_sql_manage import user, users_sql_shuju
from Day03.xuleihua import userSerializer


# Create your views here.
#登录接口，需要用户名和密码
@api_view(['POST'])
def tologin(request):
    if request.method != 'POST':
        return JsonResponse({"error":"One"})
    #获取postman 中的值
    body = request.data
    # #请求参数 pram_username
    pram_username = body.get('user')
    pram_password = body.get('pwd')
    # 使用filter返回数据是一个集合querySet
    dbRes = login.objects.filter(username=pram_username).values()
    if len(dbRes) != 0:
        return JsonResponse({"message":"用户名不能为空"})
    for i in dbRes:
        if i['password'] == pram_password:
            return JsonResponse({"username":pram_username,"password":pram_password,"status":"登录成功"})
        else:
            return JsonResponse({"status":"请输入正确的密码"})
        # if i['password'] == pram_password:
        #     return JsonResponse({"password":pram_password,"status":"登录失败"})
    # return HttpResponse('登录成功')
    # return JsonResponse({"status":"123"})

@api_view(['POST'])
def charu(request):
    if request.method != 'POST':
        return JsonResponse({"error":"不存在"})
    body = request.data
    parm_username = body['user']
    parm_password = body['pwd']
    biao = login.objects.filter(username=parm_username).values()
    if len(biao) != 0:
        return JsonResponse({"message":"用户名存在"})
    doinsert = login.objects.create(username=parm_username,password=parm_password)
    doinsert.save()
    return JsonResponse({"message":"新增成功"})


@api_view(['GET'])
def user_by_id(request):
    parm_id = request.GET.get('id')
    parm_name = request.GET.get('username')
    #校验必填项，判断列表里面每一个参数必有，【a,b,c】只要有任意一个，都可以继续执行下面逻辑
    for i in paramCheck.user_by_id_param_check:
        if not request.GET.get(i):
            return JsonResponse({"message":"参数确实或者不正确"})
    #dict 拿到所有key === list
    i  = request.GET.dict()
    print(i)
    #获取单个user，三目运算符 如果param_id不为空，执行左边，否则执行右边
    # user = login.objects.get(id=parm_id) if parm_id else login.objects.get(username=parm_name)
    user = login.objects.get(id = parm_id)
    # 硬编码---我们测试角度，不允许这么写，维护不方便，慢慢变成不可维度代码
    resdata = {
        "username":user.username,
        "password":user.password
    }
    # return JsonResponse(xuleihua(user).data)
    if user:
        return JsonResponse(resdata)
    print(type(user))
    #转换成json返回
    return HttpResponse(user)

@api_view(['get'])
def user_by_id2(request):
    param_id = request.GET.get('id')
    param_user = request.GET.get('username')
    #校验必填项
    for i in paramCheck.user_by_id_param_check:
        if not request.GET.get(i):
            return JsonResponse({"message":"参数确实或者不正确"})


    # user = login.objects.get(id=param_id)
    # user = login.objects.all().values()

    #返回用户列表

    return JsonResponse(xuleihua.userSerializer(user,many=True).data,safe=False)

@api_view(['POST'])
def users_by_id_updata(request):
    #获取id
    for i in paramCheck.user_by_id:
        if not request.GET.get(i):
            return JsonResponse({"error":"参数id不能为空"})

    id = request.data['id']
    name = request.data['name']
    #
    # user = users_sql_shuju.get(id=id)
    # #赋值
    # user.username = request.data.get('username')
    # user.save()

    #----------第二种
    usern = users_sql_shuju.filter(id=id)
    usern.update(username=name)

    return JsonResponse({"message":"修改成功"})


